<template>
  <div class="mainos">
    <div class="main_spac1 paddingBottom5">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>查看出库单</span>
          </div>
          <el-button @click="exportOrderiInfo" v-repeatClick>导出</el-button>
          <!-- <el-popconfirm
            title="您确定导出当前出库单详情吗"
            @onConfirm="exportOrderiInfo"
          >
            <el-button slot="reference">导出</el-button>
          </el-popconfirm> -->
        </div>
        <div @click="mainor_ex = false">
          <div class="main_tile boxdq marginTop10">
            <div class="main_tile_l marginLeft0"><em></em>出库单详情</div>
          </div>
          <div class="main_xq">
            <ul class="boxha">
              <li>
                出库单号：<span>{{ info.order_sn }}</span>
              </li>
              <li>
                出库类型：<span>{{ base.typeList[info.type] }}</span>
              </li>
              <li>
                创建时间：<span>{{
                  parseTime(info.created_at, "{y}-{m}-{d}")
                }}</span>
              </li>
              <li>
                出库时间：<span>{{
                  parseTime(info.updated_at, "{y}-{m}-{d}")
                }}</span>
              </li>
              <li>
                出库状态：<span>{{ base.statusList[info.status] }}</span>
              </li>
              <li>
                制单人：<span>{{ info.admin_user_name.name }}</span>
              </li>
              <li>
                关联单号：<span>{{ info.related_order_sn }}</span>
              </li>
              <li class="main_w100">
                备注：<span>{{ info.remark }}</span>
              </li>
            </ul>
          </div>
          <div class="main_tile boxdq marginTop10">
            <div class="main_tile_l marginLeft0"><em></em>商品详情</div>
            <!-- <div class="mainos_tile_r">合计：{{ total }}元</div> -->
          </div>
          <div class="main_table marginTop0">
            <el-table
              ref="multipleTable"
              v-loading="listLoading"
              :data="info.stock_out_list"
              element-loading-text="加载中"
              fit
              highlight-current-row
              :summary-method="getSummaries"
              show-summary
            >
              <el-table-column label="序号" prop="userId">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column
                label="商品名称"
                show-overflow-tooltip
                prop="name"
                class-name="main_hei"
              >
                <template slot-scope="scope"
                  >{{ scope.row.goods.name }}【{{
                    scope.row.goods_spu.spu_name.title
                  }}】</template
                >
              </el-table-column>
              <el-table-column
                label="单位"
                prop="goods_spu.spu_unit.title"
                class-name="main_hei"
              />
              <el-table-column
                label="出库数量"
                show-overflow-tooltip
                prop="num"
                class-name="main_hei"
              />
              <el-table-column
                label="单价"
                show-overflow-tooltip
                prop="price"
                class-name="main_hei"
              />
              <el-table-column
                label="出库金额"
                show-overflow-tooltip
                prop="money"
                class-name="main_hei"
              >
              </el-table-column>
              <el-table-column
                label="备注"
                prop="remark"
                class-name="main_hei"
              />
            </el-table>
          </div>
          <div class="main_tile boxdq marginTop20">
            <div class="main_tile_l marginLeft0"><em></em>生命周期</div>
          </div>
          <div class="main_table marginTop0">
            <el-table
              ref="multipleTable"
              v-loading="listLoading"
              :data="logs"
              element-loading-text="加载中"
              fit
              highlight-current-row
            >
              <el-table-column
                label="管理员"
                prop="admin.name"
                class-name="main_hei"
              />
              <el-table-column
                label="操作"
                prop="operate.name"
                class-name="main_hei"
              />
              <el-table-column
                label="内容"
                prop="content"
                class-name="main_hei"
              >
                <template slot-scope="scope">
                  <div
                    v-for="(content, index) in scope.row.content"
                    :key="index"
                    class="marginTop5 marginBottom5"
                  >
                    {{ content }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="IP" prop="ip" class-name="main_hei" />
              <el-table-column
                label="时间"
                prop="created_at"
                class-name="main_hei"
              >
                <template slot-scope="scope">{{
                  parseTime(scope.row.created_at)
                }}</template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  stockOut_base,
  stockOut_info,
  export_stockout_info
} from "@/api/warehouse/warehouseWithdrawal";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime, exportMethod } from "@/utils/index";

export default {
  computed: {
    total() {
      let sz = 0;
      this.info.stock_out_list.forEach(e => {
        sz += parseFloat(this.$toFixed(parseFloat(e.money)));
      });
      return this.$toFixed(sz);
    }
  },
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      // 基本信息
      info: {
        admin_user_name: {
          name: ""
        },
        created_at: "2000-01-01",
        updated_at: "2000-01-01",
        type: 0,
        status: 0,
        mode: 0,
        stock_out_list: []
      },
      logs: [],
      base: {
        statusList: ["后台"],
        typeList: ["待审核"]
      },
      listLoading: false,
      // id
      id: this.$route.params.id
    };
  },
  created() {
    // 基本信息
    this.stockOut_info();
    this.stockOut_base();
  },
  methods: {
    // 基本信息
    stockOut_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          )
        },
        apiUrl().name,
        { id: this.id }
      );
      stockOut_info(data).then(res => {
        if (res.errcode == 0) {
          res.data.logs.map(e => {
            if (e.content) {
              e.content = JSON.parse(e.content);
            }
          });

          this.info = res.data.info;
          this.logs = res.data.logs;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    stockOut_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name
      );
      stockOut_base(data).then(res => {
        if (res.errcode == 0) {
          this.base = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 出库单详情导出
    exportOrderiInfo() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        { id: this.id }
      );
      export_stockout_info(data).then(res => {
        this.exportMethod(res, "出库单");
      });
      //  .then((res) => {
      //     this.exportMethod(res, "出库单");
      //   })
      //   .catch(() => {
      //     this.$message.error("导出失败");
      //   });
    },
    // 计算每列的和
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总价";
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          // sums[index] += " 元";
        } else {
          // sums[index] = "N/A";
        }
      });

      sums[5] = this.total;
      sums.forEach((item, index) => {
        if (index != 0 && index != 8 && index != 5) {
          sums[index] = " ";
        }
      });
      return sums;
    }
  }
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainos {
  .main_tile {
    .mainos_tile_r {
      color: $colour_red;
      font-size: $font_14;
    }
  }
  .mainor_ex {
    position: fixed;
    width: 220px;
    height: 100%;
    background: $white;
    box-shadow: 0 6px 10px rgba(0, 21, 41, 0.08);
    top: 0;
    right: 0;
    z-index: 9999;
    transition: all 0.5s;
    transform: translate3d(100%, 0, 0);
    &.on {
      transform: translate3d(0, 0, 0);
    }
    .mainor_exb {
      position: absolute;
      width: 100%;
      height: calc(100% - 60px);
      overflow-y: auto;
      h3 {
        font-size: $font_16;
        color: $colour_18;
        height: 50px;
        line-height: 50px;
        border-bottom: 1px solid $colour_e6;
        padding-left: 20px;
      }
      .mainor_exb_an {
        padding-left: 20px;
        margin-top: 15px;
        /deep/.el-checkbox {
          display: block;
          margin-top: 15px;
          margin-bottom: 15px;
        }
      }
    }
    .mainor_exb_b {
      position: absolute;
      bottom: 0;
      width: 100%;
      border-top: 1px solid $colour_e6;
      height: 55px;
      text-align: center;
      /deep/.el-button--default {
        margin-top: 12px;
        border-color: $subActive;
        color: $menuText;
        &:hover {
          border-color: $menuActiveText;
          color: $menuActiveText;
        }
      }
    }
  }
}
</style>
